package com.htdata.htiiot.redis;

import com.htdata.htiiot.conf.Configs;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class ConRedis {
	
	   public  static Configs confRedis ;
		  
    //在borrow一个jedis实例时，是否提前进行validate操作；如果为true，则得到的jedis实例均是可用的；  
	private static boolean TEST_ON_BORROW = false; 
    
	
	private static JedisPool jedisPool = null;  
	
	
	private static Jedis jedis = null;
	private static String Addres = null;
	private static int port = 0;
	private static String pass = null;
	public ConRedis(){
		confRedis = new Configs();
		
		 Addres = confRedis.getRedisUrl();
		 port = Integer.parseInt(confRedis.getRedisPort());
		 pass = confRedis.getRedisPassword();
		
	}
	
	public static  Jedis getConn() { 
		
		   
		        //连接redis服务器，192.168.0.100:6379
		       jedis = new Jedis(Addres, port);		        //权限认证
		       jedis.auth(pass);
			  return jedis;  
	 }
	    
    
    /** 
     * 获取jedis连接池 
     * */  
 /*   public static JedisPool getPool()  
    {  
        if(jedisPool == null)  
        {  
        	//String  redisMode = ConfigClient.instance().get("redis", "mode");
    	    int redisAttempts =Integer.parseInt( ConfigClient.instance().get("redis", "maxAttempts"));
        	String redisAddres = ConfigClient.instance().get("redis", "address");
        	int redisPort = Integer.parseInt(ConfigClient.instance().get("redis", "port"));
        	String redisAuth = ConfigClient.instance().get("redis", "auth");
        	int redisConTimeOut =Integer.parseInt( ConfigClient.instance().get("redis", "connectionTimeout"));
        	int redisSoTimeout = Integer.parseInt(ConfigClient.instance().get("redis", "soTimeout"));	
    				 
            //创建jedis连接池配置  
			 JedisPoolConfig redisConf = new JedisPoolConfig();  
			 redisConf.setMaxIdle(redisAttempts);  
			 redisConf.setMaxWaitMillis(redisConTimeOut); 
			 redisConf.setTestOnBorrow(TEST_ON_BORROW); 
			 jedisPool = new JedisPool(redisConf, redisAddres, redisPort, redisSoTimeout, redisAuth);
        }  
        return jedisPool;  
    }  
     */ 
    /** 
     * 获取jedis连接 
     * */  
   /* public  Jedis getConn()  
    {  
        return getPool().getResource();  
    }  
    */
    
  

}  
	

